﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using EF.Model;
using EF.Model.Base;
using EF.Util;
using EF.Util.DBUtility;
using EF.BLL.Base;

public partial class Trans_Base_Role : BasePage
{
    private RoleService roleBLL = new RoleService();

    int userId = 0; //用户Id
    string role = ""; //角色
    string lang = ""; //语言

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckSession();

        userId = Convert.ToInt32(Session["uid"].ToString()); //用户Id
        role = Session["role"].ToString(); //角色
        lang = Session["lang"].ToString(); //语言

        Response.Clear();

        switch (Request["action"].ToString().ToLower())
        {
            case "func_tree":
                GetFunctionTree(); //获取功能点树
                break;
            case "func_set":
                SetFunctionRole(); //获取功能点树
                break;
            case "short_list":
                GetShortList();
                break;
            case "export": //导出
                Export();
                break;
            case "model":
                GetModel();
                break;
            case "role_list":
                GetRoleList();
                break;
            case "add":
                Add(); //1,新建成功;2,新建失败,用户名已存在;0,新建失败,数据库异常.
                break;
            case "edit":
                Edit(); //1,编辑成功;0,编辑失败,数据库异常.
                break;
            case "logicdel": //1,逻辑删除成功;0,逻辑删除失败,数据库异常.
                LogicDel();
                break;
            default:
                break;
        }

        Response.End();
    }

    private void GetShortList()
    {
        try
        {
            DataTable dt = roleBLL.GetShortList(lang);
            Response.Write("{success:true,rows:" + JsonHelp.WriteDataTable(dt) + "}");
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void Export()
    {
        try
        {

        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void GetRoleList()
    {
        try
        {
            int rowsCount; //总记录数

            int? pageSize = Request["limit"] == null ? Convert.ToInt32(ConfigurationManager.AppSettings["PageSize"].ToString()) : Convert.ToInt32(Request["limit"].ToString());
            int? pageIndex = Request["start"] == null ? 1 : Convert.ToInt32(Request["start"].ToString()) / pageSize + 1;

            string sort = Request["sort"] == null ? "UpdateTime" : Request["sort"].ToString();
            string dir = Request["dir"] == null ? "DESC" : Request["dir"].ToString();

            string columns = "RoleId,RoleName,RoleMark,RoleDescr,SortIndex,CreateTime,UpdateTime"; //返回的列

            DataTable dt = roleBLL.GetRoleList(lang, columns, pageIndex, pageSize, sort + " " + dir, out rowsCount);

            Response.Write("{success:true,rowsCount:" + rowsCount + ",rows:" + JsonHelp.WriteDataTable(dt) + "}");


        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void GetModel()
    {
        try
        {
            int selRoleId = Convert.ToInt32(Request["sel_role_id"].ToString());
            RoleModel model = roleBLL.GetModel(lang, selRoleId);

            Response.Write("{success:true,rows:" + JsonHelp.Convert2Json(model) + "}");
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void Add() //1,新建成功;2,新建失败,用户名已存在;0,新建失败,数据库异常.
    {
        try
        {
            RoleModel model = (RoleModel)JsonHelp.Convert2Entity(Request["model"].ToString(), typeof(RoleModel));
            model.CreateUser = userId;

            string msg;
            int result = roleBLL.Add(model, lang, out msg);
            Response.Write("{success:true,result:" + result + ",msg:'" + msg + "'}");
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void Edit() //1,编辑成功;0,编辑失败,数据库异常.
    {
        try
        {
            RoleModel model = (RoleModel)JsonHelp.Convert2Entity(Request["model"].ToString(), typeof(RoleModel));
            model.UpdateUser = userId;

            string msg;
            Response.Write("{success:true,result:" + roleBLL.Edit(model, lang, out msg) + ",msg:'" + msg + "'}");
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void LogicDel() //1,逻辑删除成功;0,逻辑删除失败,数据库异常.
    {
        try
        {
            string selRoleIds = Request["sel_role_id"].ToString();

            string msg;
            Response.Write("{success:true,result:" + roleBLL.LogicDel(selRoleIds, userId, lang, out msg) + ",msg:'" + msg + "'}");

        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void SetFunctionRole() //设置权限
    {
        try
        {
            string msg;
            string FuncRoleId = Request["func_role_id"].ToString();
            int selRoleId = Convert.ToInt32(Request["sel_role_id"].ToString());
            int result = roleBLL.SetFunctionRole(FuncRoleId, selRoleId, userId, lang, out msg);
            Response.Write("{success:true,result:" + result + ",msg:'" + msg + "'}");

        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }

    private void GetFunctionTree() //获取功能点树
    {
        try
        {
            int selRoleId = Convert.ToInt32(Request["sel_role_id"].ToString());
            DataTable dt = roleBLL.GetFunctionTree(selRoleId, lang);

            string treeJson = "[" + JsonHelp.ConvertToTreeJson(dt, "id", "FatherId", 0) + "]";

            treeJson = treeJson.Replace("'false'", "false");
            treeJson = treeJson.Replace("'true'", "true");

            Response.Write(treeJson);

        }
        catch (Exception ex)
        {
            Log.WriteLog(ex);
            Response.Write(GetErrorJson());
        }
    }
}
